package team.se.tms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.dao.impl.DaoImpl;
import team.se.tms.dto.Page;
import team.se.tms.dto.ParameterPage;
import team.se.tms.exception.InsertException;
import team.se.tms.po.CourseInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.service.CourseInfoService;
import team.se.tms.util.KeyUtil;

import java.lang.reflect.Parameter;
import java.util.List;

/**
 * Created by argentum on 2018/11/28.
 */
@Service("courseInfoService")
public class CourseInfoServiceImpl extends BaseServiceImpl implements CourseInfoService{

    @Autowired
    DaoImpl dao;

    /**
     * @create on 2018/11/29 by argentum
     * @update on 2018/11/29 by argentum
     *
     * @param courseInfo
     * @return
     * @throws Exception
     */

    @Override
    public Boolean saveCourseInfo(CourseInfo courseInfo) throws Exception {
        courseInfo.setCourseId(KeyUtil.generatePrimaryKey());
        int count = dao.insert("CourseInfoMapper.insertCourseInfo",courseInfo);
        if(count<=0){
            throw new InsertException();
        }
        return true;
    }

    /**
     * 根据人才培养方案id获取课程信息
     *
     * @param talentTrainingProgramId 人才培养方案id
     * @param page 分页
     * @return 课程信息列表
     * @throws Exception 异常
     * @create on 2019/3/21 by winkyle
     */
    @Override
    public List<CourseInfo> listCourseInfos(@SpecialCheck("Pk")String talentTrainingProgramId, Page page) throws Exception {
        return dao.selectList("CourseInfoMapper.selectCourseInfosByTalentTrainingProgramId",getParameterPage(page,"talentTrainingProgramId",talentTrainingProgramId));
    }

    /**
     * 修改课程信息
     * @param courseInfo
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @Override
    public boolean modifyCourseInfo(CourseInfo courseInfo) throws Exception {
        int result = dao.update("CourseInfoMapper.updateCourseInfoByCourseId",courseInfo);
        if (result != 0){
            return true;
        }
        return false;
    }

    /**
     * 删除课程信息
     * @param courseId
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @Override
    public int removeCourseInfo(String courseId) throws Exception {
        List<TeachCourseInfo> teachCourseInfos = dao.selectList("TeachCourseInfoMapper.selectTeachCourseInfoByCourseId",courseId);
        if (teachCourseInfos.isEmpty()){
            return dao.delete("CourseInfoMapper.deleteCourseInfoByCourseId",courseId);
        }
        return -1;
    }
}
